package App.Service;

import App.Job.AuthJob;
import App.Job.MyJob;
import App.Job.TaskReminderJob;
import App.Model.Task.CrmTaskSchedule;
import App.Repository.ScheduleRepository;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by xw on 2019/1/7.
 */

@Service
public class ScheduleService {

    @Autowired
    SchedulerFactoryBean schedulerFactoryBean;

    public void loadTaskSchedule(String url, String username, String password) {

        try {

            // Grab the Scheduler instance from the Factory
            // Scheduler scheduler = schedulerFactoryBean.getScheduler();
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.clear();

            // and start it off
            scheduler.start();

            ScheduleRepository _repostory = new ScheduleRepository();
            List<CrmTaskSchedule> _scheduleList = _repostory.selectTaskSchedule(url, username, password);
            System.out.println("_scheduleList size:" + _scheduleList.size());
           /* for (CrmTaskSchedule s : _scheduleList) {

                String _recid = s.getRecid();
                String _reminderid = s.getReminderid();
                String _cronString = s.getCronstring();


                JobDetail _job2 = JobBuilder.newJob(MyJob.class)
                        .withDescription("任务定时job") //job的描述
                        .withIdentity("taskJob" + _recid, "taskJobGroup") //job 的name和group
                        .build();

                _job2.getJobDataMap().put("reminderid", _reminderid);

                Date _startDatetime = s.getBegintime();
                Trigger _trigger2 = TriggerBuilder.newTrigger()
                        .withDescription("this is schedule trigger")
                        .withIdentity("taskTrigger" + _recid, "taskTriggerGroup")
                        .startAt(_startDatetime)  //默认当前时间启动
                        .withSchedule(CronScheduleBuilder.cronSchedule(_cronString))
                        .build();

                scheduler.scheduleJob(_job2, _trigger2);
            }*/

            addAuthUserJob(scheduler);

            addTaskReminder(scheduler);

        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }


    private void addAuthUserJob(Scheduler scheduler) throws Exception {
        String _recid = UUID.randomUUID().toString();
        String _cronString = "0 */1 * * * ?";

        JobDetail _job2 = JobBuilder.newJob(AuthJob.class)
                .withDescription("任务定时job") //job的描述
                .withIdentity("taskJob" + _recid, "taskJobGroup") //job 的name和group
                .build();

        _job2.getJobDataMap().put("name", _recid.toString());

        Date _now = new Date();
        Trigger _trigger2 = TriggerBuilder.newTrigger()
                .withDescription("")
                .withIdentity("taskTrigger" + _recid, "taskTriggerGroup")
                .startAt(_now)  //默认当前时间启动
                .withSchedule(CronScheduleBuilder.cronSchedule(_cronString)) //两秒执行一次
                .build();

        scheduler.scheduleJob(_job2, _trigger2);

    }

    private void addTaskReminder(Scheduler scheduler) throws Exception {
        String _recid = UUID.randomUUID().toString();
        String _cronString = "0 */1 * * * ?";

        JobDetail _job2 = JobBuilder.newJob(TaskReminderJob.class)
                .withDescription("任务定时job") //job的描述
                .withIdentity("taskJob" + _recid, "taskJobGroup") //job 的name和group
                .build();

        _job2.getJobDataMap().put("name", _recid.toString());

        Date _now = new Date();
        Trigger _trigger2 = TriggerBuilder.newTrigger()
                .withDescription("")
                .withIdentity("taskTrigger" + _recid, "taskTriggerGroup")
                .startAt(_now)  //默认当前时间启动
                .withSchedule(CronScheduleBuilder.cronSchedule(_cronString)) //两秒执行一次
                .build();

        scheduler.scheduleJob(_job2, _trigger2);

    }
}
